class Solution(object):
    def twoSum(self, nums, target):
        index1=0
        index2=0
        for i in range(len(nums)):
            j=i+1
            for j in range(len(nums)):
                if (nums[i]+nums[j])==target:
                    index1=i
                    index2=j
                    print(index1,index2)
                    return [index1,index2]   

class Solution2(object):
    def twoSum(self, nums, target):
        map={}
        for i in range(len(nums)):
            #计算补数
            preResult=target-nums[i]
            #如果补数在map中，返回补数和当前索引
            if preResult in map:
                return [map[preResult],i]
            #如果补数不在map中，将当前数值和索引加入map
            map[nums[i]]=i
        return []

# 测试
print("\n测试用例：")
s1 = Solution()
s2 = Solution2()
test_cases = [
    ([2, 7, 11, 15], 9),
    ([3, 2, 4], 6),
    ([3, 3], 6),
    ([1, 2, 3], 10),
]

for nums, target in test_cases:
    result = s2.twoSum(nums, target)
    if result:
        i, j = result
        print(f"✓ {nums}, target={target} → {result}")
        print(f"  验证: nums[{i}] + nums[{j}] = {nums[i]} + {nums[j]} = {nums[i] + nums[j]}")
    else:
        print(f"✗ {nums}, target={target} → 未找到")

print("\n" + "=" * 70)
print("【方案2：哈希表法 - O(n) 时间，O(n) 空间】（推荐）")
print("=" * 70)

